package com.example.orderapplication

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.orderapplication.bean.UserData
import com.example.orderapplication.helper.DataHelper
import java.util.regex.Pattern

class RegisterActivity : AppCompatActivity() {
    private val REQUEST_CODE = 1024
    private val TAG = "RegisterActivity"

    private val mEtName: EditText by lazy { findViewById(R.id.et_name) }
    private val mEtPhoneNum: EditText by lazy { findViewById(R.id.et_phone_num) }
    private val mEtPassword: EditText by lazy { findViewById(R.id.et_password) }
    private val mEtNextPassword: EditText by lazy { findViewById(R.id.et_next_password) }
    private val mBtRegister: Button by lazy { findViewById(R.id.bt_register) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        requestPermission()
        mBtRegister.setOnClickListener {
            val name: String = mEtName.text.toString()
            val phone = mEtPhoneNum.text.toString()
            val password = mEtPassword.text.toString()
            val nextPassword = mEtNextPassword.text.toString()
            if (name.isEmpty()) {
                Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (phone.isEmpty() || !isPhoneNumber(phone)) {
                Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (password.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (nextPassword.isEmpty()) {
                Toast.makeText(this, "请再次输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (nextPassword != password) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val dataBaseHelper = DataHelper(this)
            val f = UserData()
            f.name = name
            f.phone = phone
            f.password = password
            Log.e(TAG, name)
            Log.e(TAG, phone)
            Log.e(TAG, password)
            if (dataBaseHelper.insertFilmUserData(f)) {
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show()
            }


        }
    }

    private fun isPhoneNumber(input: String): Boolean {
        val regex = "(1[0-9][0-9]|15[0-9]|18[0-9])\\d{8}"
        val p = Pattern.compile(regex)
        return Pattern.matches(regex, input)
    }

    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                startActivityForResult(intent, REQUEST_CODE)
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
            } else {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ),
                    REQUEST_CODE
                )
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                Toast.makeText(this, "存储权限获取成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                Toast.makeText(this, "存储权限获取成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
}